<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('名称')" maxlength="50" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('地点')" prop="place">
            <el-input v-model="form.place" :placeholder="$t('地点')" maxlength="50" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('电话')" prop="phone">
            <el-input v-model="form.phone" :placeholder="$t('电话')" maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最大容纳人数')" prop="largestNumber">
            <el-input-number v-model="form.largestNumber" :controls="false" :precision="0" :max="99999" :placeholder="$t('最大容纳人数')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否有电话会议')" prop="callMeeting">
            <el-radio-group v-model="form.callMeeting">
              <el-radio
                v-for="dict in callMeetingOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否有视频会议')" prop="videoMeeting">
            <el-radio-group v-model="form.videoMeeting">
              <el-radio
                v-for="dict in videoMeetingOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <SaveBtn has-permi="oa:meetingroom" />
    </div>
  </div>
</template>

<script>
import { getOaMeetingRoom, saveOaMeetingRoom } from '@/api/oa/meeting/oaMeetingRoom'
    export default {
        data() {
          const checkPhone = (rule, value, callback) => {
            if (value !== undefined && value !== '' && value !== null) {
              // 校验电话
              if (rule.field === 'phone') {
                var regPhone = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/
                if (!regPhone.test(value)) {
                  callback(new Error(this.$t('号码有误格式不正确：0000-0000000')))
                }
              }
            }
            callback()
          }
            return {
                // 是否有电话会议字典
                callMeetingOptions: [], // 是否有视频会议字典
                videoMeetingOptions: [], // 表单参数
                form: {},
                loading: false,
                // 表单校验
                rules: {
                  name: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  place: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  phone: [
                    { required: false, validator: checkPhone,	trigger: 'blur' }
                  ]
                }
            }
        },
        created() {
          this.getDicts('sys_yes_no').then(response => {
              this.callMeetingOptions = response.data
          })
          this.getDicts('sys_yes_no').then(response => {
              this.videoMeetingOptions = response.data
          })
        },
        methods: {
          init(data) {
            this.loading = true
            if (data != null && data.id !== undefined) {
              getOaMeetingRoom(data.id).then(response => {
                this.form = response.data
                this.loading = false
              })
            } else {
              this.reset()
              this.loading = false
            }
          },
          // 表单重置
          reset() {
            this.form = {
              id: undefined,
              name: undefined,
              place: undefined,
              phone: undefined,
              largestNumber: undefined,
              callMeeting: undefined,
              videoMeeting: undefined,
              createBy: undefined,
              createTime: undefined,
              updateBy: undefined,
              updateTime: undefined,
              delFlag: undefined
            }
            this.resetForm('form')
          },
            // 取消按钮
            cancel() {
                this.$parent.$parent.open = false
                this.$parent.$parent.reset()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                  if (valid) {
                      this.loading = true
                      saveOaMeetingRoom(this.form).then(response => {
                        if (response.code === 200) {
                          this.msgSuccess(this.$t('保存成功'))
                          this.$parent.$parent.formOpen = false
                          this.$parent.$parent.getList()
                        } else {
                          this.msgError(response.msg)
                        }
                        this.loading = false
                      }).catch(() => {
                        this.loading = false
                      })
                  } else {
                    this.locationError(document)
                  }
              })
           },
          checkPhone: function(rule, value, callback) {
            if (value !== undefined && value !== '' && value !== null) {
              // 校验电话
              if (rule.field === 'phone') {
                var regPhone = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/
                if (!regPhone.test(value)) {
                  callback(new Error(this.$t('号码有误格式不正确')))
                }
              }
            }
            callback()
          }

        }

    }
</script>
